package com.wsc.wisdomchinaqinghe.activitity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.javaBean.YuYueCenterDetailBean;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.MyToast;


public class YuYueCenterDetailActivity extends BaseActivity implements View.OnClickListener,ContributorView {

    private ImageView regist_back;
    private TextView tv_headtitle;
    private ContributorPresenter contributorPresenter;
    private Context context;
    private MyToast myToast;
    private String id;
    private final String ApplyCenterFlag=YuYueCenterDetailActivity.class.getSimpleName();
    private ImageView icon;
    private TextView title;
    private TextView time;
    private TextView tv_yuyuetime;
    private TextView tv_name;
    private TextView tv_phone;
    private TextView tv_address;
    private TextView tv_ayinumber;
    private TextView tv_dashaotime;
    private TextView tv_shangmentime;
    private TextView tv_remark;
    private TextView tv_state;
    private RelativeLayout layout_dashaotime;
    private RelativeLayout layout_ayishuliang;
    private TextView tv_timedesc;
    private TextView tv_delete;
    private final String DELETEHOUSEDELETEFLAG="DELETEHOUSEDELETEFLAG";
    private RelativeLayout layout_state;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yuyuecenterdetail_layout);
        initView();
        Intent intent=getIntent();
        context=this;
        myToast=new MyToast(context);
        contributorPresenter = new ContributorPresenter(this, context);
        id=intent.getStringExtra("id");
        contributorPresenter.getMyModuleApplyInfo(id,ApplyCenterFlag);
    }

    private void initView()
    {
        regist_back=(ImageView) findViewById(R.id.regist_back);
        regist_back.setOnClickListener(this);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText("预约详情");
        tv_headtitle.requestFocus();
        tv_headtitle.setFocusable(true);
        tv_headtitle.setFocusableInTouchMode(true);
        icon=(ImageView) findViewById(R.id.icon);
        title=(TextView) findViewById(R.id.title);
        time=(TextView) findViewById(R.id.time);
        tv_yuyuetime=(TextView) findViewById(R.id.tv_yuyuetime);
        tv_name=(TextView) findViewById(R.id.tv_name);
        tv_phone=(TextView) findViewById(R.id.tv_phone);
        tv_address=(TextView) findViewById(R.id.tv_address);
        tv_ayinumber=(TextView) findViewById(R.id.tv_ayinumber);
        tv_dashaotime=(TextView) findViewById(R.id.tv_dashaotime);
        tv_shangmentime=(TextView) findViewById(R.id.tv_shangmentime);
        tv_remark=(TextView) findViewById(R.id.tv_remark);
        tv_state=(TextView) findViewById(R.id.tv_state);
        layout_dashaotime=(RelativeLayout) findViewById(R.id.layout_dashaotime);
        layout_ayishuliang=(RelativeLayout) findViewById(R.id.layout_ayishuliang);
        tv_timedesc=(TextView) findViewById(R.id.tv_timedesc);
        tv_delete=(TextView) findViewById(R.id.tv_delete);
        tv_delete.setOnClickListener(this);
        layout_state=(RelativeLayout) findViewById(R.id.layout_state);
        layout_state.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.regist_back:
                finish();
                break;
            case R.id.tv_delete:
                contributorPresenter.getDelMyApplyById(id,DELETEHOUSEDELETEFLAG);
                break;
            case R.id.layout_state:
                if(tv_state.getText().toString().trim().equals("待评价"))
                {
                    Intent intent=new Intent(context,PingjiaDetailActivity.class);
                    intent.putExtra("id",id);
                    startActivityForResult(intent,0);
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==0 && resultCode==1)
        {
            contributorPresenter.getMyModuleApplyInfo(id,ApplyCenterFlag);
        }
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if(flag.equals(ApplyCenterFlag)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    YuYueCenterDetailBean yuYueCenterDetailBean=JsonTools.jsonStringToObject(jsonMessage,YuYueCenterDetailBean.class);
                    Glide.with(context).load(yuYueCenterDetailBean.getImg())
                            .into((ImageView) icon);

                    title.setText(yuYueCenterDetailBean.getServerName());
                    if(title.getText().toString().equals("保洁服务"))
                    {
                        layout_dashaotime.setVisibility(View.VISIBLE);
                        layout_ayishuliang.setVisibility(View.VISIBLE);
                        tv_ayinumber.setText(yuYueCenterDetailBean.getAcntNumber()+"位");
                        tv_dashaotime.setText(yuYueCenterDetailBean.getPlayTime()+"小时");
                        tv_timedesc.setText("上门时间");
                    }else {
                        layout_dashaotime.setVisibility(View.GONE);
                        layout_ayishuliang.setVisibility(View.GONE);
                        if (title.getText().toString().equals("月嫂保姆"))
                        {
                            tv_timedesc.setText("面试时间");
                        }else {
                            tv_timedesc.setText("上门时间");
                        }
                    }

                    time.setText(Tools.formatDateTimeyyyyMMDDhhmm(Tools.formatData(yuYueCenterDetailBean.getServerCreateDate())));
                    tv_yuyuetime.setText(Tools.formatDateTimeyyyyMMDDhhmm(Tools.formatData(yuYueCenterDetailBean.getCreateDate())));
                    tv_name.setText(yuYueCenterDetailBean.getApplyName());
                    tv_phone.setText(yuYueCenterDetailBean.getApplyPhone());
                    tv_address.setText(yuYueCenterDetailBean.getAddress());

                    tv_shangmentime.setText(Tools.formatDateTimeyyyyMMDDhhmm(Tools.formatData(yuYueCenterDetailBean.getUpDoorDate())));
                    switch (yuYueCenterDetailBean.getState()) {
                        case 0:
                            tv_state.setText("待处理");
                            tv_state.setTextColor(context.getResources().getColor(R.color.D7F2));
                            tv_delete.setVisibility(View.VISIBLE);
                            break;
                        case 1:
                            tv_state.setText("已接单");
                            tv_state.setTextColor(context.getResources().getColor(R.color.D7F2));
                            tv_delete.setVisibility(View.GONE);
                            break;
                        case 2:
                            tv_state.setText("待评价");
                            tv_state.setTextColor(context.getResources().getColor(R.color.CD1F));
                            tv_state.setCompoundDrawablesWithIntrinsicBounds(null,null,context.getResources().getDrawable(R.mipmap.go2x),null);
                            tv_state.setCompoundDrawablePadding(Tools.dip2px(context,10));
                            tv_delete.setVisibility(View.GONE);
                            break;
                        case 3:
                            tv_state.setText("失败");
                            tv_state.setTextColor(context.getResources().getColor(R.color.FC5812));
                            tv_delete.setVisibility(View.VISIBLE);
                            break;
                        case 4:
                            tv_state.setText("完成");
                            tv_state.setTextColor(context.getResources().getColor(R.color.CD1F));
                            tv_delete.setVisibility(View.GONE);
                            break;

                    }
                    tv_remark.setText(yuYueCenterDetailBean.getRemarks());
                }
            }else if(flag.equals(DELETEHOUSEDELETEFLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = (String) object;
                    if(jsonMessage.equals("41"))
                    {
                        myToast.showShort("删除成功");
                        finish();
                    }else {
                        myToast.showShort("删除失败");
                    }
                }else
                {
                    myToast.showShort("删除失败");
                }
            }

        } catch (Exception e) {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }
}
